//
//  LFOriginalView.swift
//  SDAutoWeibo
//
//  Created by 刘健 on 16/3/25.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit

import SnapKit

class LFOriginalView: UIView {
    
    var OriginalBottomConstraint: Constraint?
    
    var status : LFStatus? {
        didSet {
            //设置头像
            if let imageString = status?.user?.profile_image_url {
                
              let URL =  NSURL(string: imageString)
                iconView.sd_setImageWithURL(URL, placeholderImage: UIImage(named: "avatar"))
            }
            //设置名称
          userNameLabel.text = status?.user?.name
            
            //设置VIP
            if let image = status?.user?.mbrankImage {
                mbrankView.image = image
            }else {
                mbrankView.image = nil
            }
 
            //认证视图
            verifiedView.image = status?.user?.verifiedImage
            
            //设置发表时间
            createdTimeLabel.text = NSDate.compareSendContentTime(status!.created_at!).sinaDateDescripiton()
                //设置来源
            sourceLabel.text = status?.source
            
            //设置内容
            contentLabel.text = status?.text
            
            //删除之前的约束
            OriginalBottomConstraint?.uninstall()
            if let thumbnailPicURLs = status?.thumbnailPicURLs {
                
//                print("\(thumbnailPicURLs)")
                
                pictureView.hidden = false
                
                //给 picView.thumbnailPicURLs赋值
                pictureView.picURLs = thumbnailPicURLs
                //重写约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                self.OriginalBottomConstraint = make.bottom.equalTo(pictureView).offset(LFOffSetDefault).constraint
                })
            }else {
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                self.OriginalBottomConstraint = make.bottom.equalTo(contentLabel).offset(LFOffSetDefault).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
      //MARK: -- 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        prepareUI()
    }
    
      //MARK: -- UI布局
    private func prepareUI() {
        
        addSubview(iconView)
        addSubview(userNameLabel)
        addSubview(createdTimeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(mbrankView)
        addSubview(verifiedView)
        addSubview(pictureView)
        
        
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(LFOffSetDefault)
            make.leading.equalTo(self).offset(LFOffSetDefault)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        
        
        //用户名称
        userNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(LFOffSetDefault)
        }
        
        //发表时间
        createdTimeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(LFOffSetDefault)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(createdTimeLabel)
            make.leading.equalTo(createdTimeLabel.snp_trailing).offset(LFOffSetDefault)
        }
        
        //VIP
        mbrankView.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(userNameLabel)
            make.leading.equalTo(userNameLabel.snp_trailing).offset(LFOffSetDefault)
            make.size.equalTo(CGSizeMake(14, 14))
        })
        
        //认证视图
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSizeMake(17, 17))
        }

        
        //微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(LFOffSetDefault)
            make.leading.equalTo(iconView)
            make.trailing.equalTo(self).offset(-LFOffSetDefault)
        }
        
        //配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(LFOffSetDefault)
            make.leading.equalTo(self).offset(LFOffSetDefault)
            make.size.equalTo(CGSizeMake(200, 200))
        }
    
        //self的约束
        self.snp_makeConstraints { (make) -> Void in
            //当配图没有时，就要去依赖label底部
         self.OriginalBottomConstraint = make.bottom.equalTo(pictureView).offset(LFOffSetDefault).constraint
        }
    }
    
    //MARK: -- 懒加载
    //头像
    private lazy var iconView = UIImageView(image: UIImage(named:"avatar"))
    
    //VIP
    private lazy var mbrankView = UIImageView()
    
    //认证视图
    private lazy var verifiedView = UIImageView()
  
    //用户名称
    private lazy var userNameLabel : UILabel = {
        let label = UILabel()
        
//        label.text = "Clemmie"
        label.font = UIFont.systemFontOfSize(16)
        label.textColor = UIColor.blackColor()
        label.sizeToFit()
        
        return label
    }()
    
    //created_at 创建时间label
    private lazy var createdTimeLabel : UILabel = {
        let label = UILabel()
        
//        label.text = "刚刚"
        label.font = UIFont.systemFontOfSize(11)
        label.textColor = UIColor.orangeColor()
        label.sizeToFit()
        
        return label
    }()
    
    //sourceLabel 
    private lazy var sourceLabel : UILabel = {
        let label = UILabel()
        
//        label.text = "www.baidu.com"
        label.font = UIFont.systemFontOfSize(9)
        label.textColor = UIColor.lightGrayColor()
        label.sizeToFit()
        
        return label
    }()

    //微博信息内容
    private lazy var contentLabel : UILabel = {
        let label = UILabel()
        
//        label.text = "我是微博我是微博我是微博我是微博我是微博我是微博我是微博我是微博我是微博我是微博我是微博我是微博我是微博我是微博我是微博"
        label.font = UIFont.systemFontOfSize(16)
        label.textColor = UIColor.lightGrayColor()
        label.sizeToFit()
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * LFOffSetDefault
        
        return label
    }()
    
    //配图
    private lazy var pictureView : LFPictureView = LFPictureView()
 
}
